<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . 
		'/SAGER/model/interfaces/IResourcesRequestAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class MySqlResourcesRequestAccessDao implements IResourcesRequestAccessDao {
  		/**
		 * Constructor
		 */
		 public function __construct () {}
	 
	  	/**
		 * Creates a resources request
		 */
		public function createResourcesRequest($resourcesRequest) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT MAX(pk_number) * -1 pk_number FROM REQUEST";			
			$sentence = mysql_query($query, $conn);			
			$row = mysql_fetch_array($sentence);
			$pkNumber = $row['pk_number'];
			
			$query = "SELECT MAX(pk_control_number) * -1 pk_control_number FROM REQUEST";
			$sentence = mysql_query($query, $conn);			
			$row = mysql_fetch_array($sentence);
			$pkControlNumber = $row['pk_control_number'];
			
			$date = $resourcesRequest -> getDate();
			$readStatus = "NO LEIDO";
			$type = "RESOURCE";
			$fkUsername = $resourcesRequest -> getUsername();
			
			$query = "INSERT INTO REQUEST (pk_number, pk_control_number, date, read_status, 
					  type, fk_username)
					  VALUES ($pkNumber, $pkControlNumber, '$date', '$readStatus', '$type', 
					  '$fkUsername')";
					  
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			$GLOBALS['debug'] -> log($sentence, "sentence");
			
			if ($sentence)
				return 1;
			
			return 0;
		 }
	}
?>